<template>
	<div class="fav-list">
		<div class="item" v-for="item in dataSource.list" :key="item._id">
			<div class="cover">
				<image :src="IMG_URL + item.goodsDetail[0].goodsImg" mode="aspectFit" />
			</div>
			<div class="info">
				<div class="name">{{ item.goodsDetail[0].goodsName }}</div>
				<button class="btn" @click="del(item.goodsDetail[0]._id)">删除</button>
			</div>
		</div>
	</div>
</template>

<script setup>
	import {
		reactive
	} from "vue"
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		IMG_URL,
		BASE_URL
	} from "@/config/index.js"

	const dataSource = reactive({
		list: []
	})

	const getList = () => {
		const {
			token
		} = uni.getStorageSync('USER_TOKENS') || {}
		uni.request({
			url: `${BASE_URL}find`,
			header: {
				Authorization: `bearer ${token}`
			},
			success: res => {
				const {
					data
				} = res.data
				dataSource.list = [.data]
			}
		});
	}

	const del = (id) => {
		const {
			token
		} = uni.getStorageSync('USER_TOKENS') || {}
		uni.request({
			url: `${BASE_URL}delfav`,
			header: {
				Authorization: `bearer ${token}`
			},
			data: {
				id
			},
			success: (res) => {
				dataSource.list = []
				getList()
			}
		})
	}

	onLoad(() => {
		getList()
	})
</script>

<style lang="scss" scoped>
	.fav-list {
		box-sizing: border-box;
		padding: 20rpx;

		.item {
			display: flex;
			border-bottom: 1rpx solid #eee;
			position: relative;
			height: 400rpx;

			.cover {
				width: 400rpx;
				height: 400rpx;

				image {
					width: 100% !important;
					height: 100% !important;
				}
			}

			.info {
				padding-left: 20rpx;

				.name {
					font-size: 36rpx;
					font-weight: bolder;
				}

				.btn {
					position: absolute;
					right: 20rpx;
					bottom: 20rpx;
					background-color: #df0b47;
					color: #fff;
					line-height: 1;
					padding: 10rpx 20rpx;
				}
			}
		}
	}
</style>